<?php
$blogdata = 'NULL';
$blog = 'NULL';
require('private.php');
require(CONFIGPATH . 'wp-config.php');
require_once( ABSPATH . WPINC . '/registration-functions.php');

get_currentuserinfo();

$user_admin = $userdata->user_admin;
$action = $_REQUEST['action'];
if ($user_login){
	if (1 != $user_admin){
		if( get_settings('users_can_create_blogs' )){
			if ( count(get_userblogs()) < get_settings('users_can_create_num_blogs') || 0 == get_settings('users_can_create_num_blogs') )
				$action = ('registerNewBlog' == $action ? 'registerNewBlog' : 'addblog');
			else
				$action = 'toomanyblogs';
		} else {
			$action = 'blogdisabled';
		}
	}
} elseif (! get_settings('users_can_register')  ){
	$action = 'disabled';
}


header( 'Content-Type: text/html; charset='.get_settings('blog_charset') );

switch( $action ) {
	case 'registerNewBlog':
		auth_redirect();
		$user_login = $_POST['user_login'];
		$user_password = $_POST['user_password'];
		$user_email = $_POST['user_email'];
		$blog_name = $_POST['blog_name'];

		$errors = array();

		if (!wp_login($user_login, $user_password, false))
			$errors['password'] = __('<strong>ERROR</strong>: Invalid password entered.<br/>');
		//this could be a little cleaner. future wp versions clean up this file, so after merging with 2.1, a refactor is in order.
		if ( !slugAvailable($blog_name) )
			$errors['blog_name'] = __('<strong>ERROR</strong>: This blog address is already taken, please choose another one.<br/>');
		if (!validateSlug($blog_name))
			$errors['blog_name'] = __('<strong>ERROR</strong>: This blog address is invalid, please choose another one.<br/>');
		if ( 0 == count($errors) ) {
			$blog_id = lyceum_create_blog( $user_ID, $blog_name );
			if ( !$blog_id)
				$errors['user_id'] = sprintf(__('<strong>ERROR</strong>: Couldn&#8217;t create blog... please contact the <a href="mailto:%s">webmaster</a> !<br/>'), 'nobody@example.com');
		}

		if ( 0 == count($errors) ) {

		?>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Lyceum &raquo; <?php _e('New Blog Created') ?></title>
		<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?>" />	
		<link rel="stylesheet" href="<?php echo WEBROOT; ?>/wp-admin/wp-admin.css" type="text/css" />
		<style type="text/css">
		.submit {
			font-size: 1.7em;
		}
		</style>
	</head>
	<body>

	<div id="login"> 
		<h2><?php _e('New Blog Created') ?></h2>
		<p><?php printf(__('Username: %s'), "<strong>$user_login</strong>") ?><br />
		<?php printf(__('E-mail: %s'), "<strong>$user_email</strong>") ?><br />
		<?php printf(__('Blog Address: %s'), "<strong>$blog_name</strong>") ?>
		</p>
		<p class="submit"><a href="profile"><?php _e('View Profile'); ?> &raquo;</a></p>
	</div>
	</body>
	</html>
			<?php
			break;
		}

	case 'addblog': // if user is creating new blog with current account
		auth_redirect();
	?>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>Lyceum &raquo; <?php _e('Registration Form') ?></title>
		<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?>" />
		<link rel="stylesheet" href="<?php echo WEBROOT; ?>/wp-admin/wp-admin.css" type="text/css" />
		<style type="text/css">
		#user_email, #user_login, #submit {
			font-size: 1.7em;
		}
		</style>
		<script src="<?php echo WEBROOT ?>/wp-includes/js/prototype.js" type="text/javascript"></script>
	</head>

	<body>
	<div id="login">
		<h1><a href="http://lyceum.ibiblio.org/">Lyceum</a></h1>
		<h2><?php _e('Create New Blog') ?></h2>
		You are logged in as: <strong><?php echo $user_login ?></strong> (<a href="login?action=logout">logout</a>)<br/><br/>
		<?php if ( isset($errors) ) : ?>
		<div class="error">
			<ul>
			<?php
			foreach($errors as $error) echo "<li>$error</li>";
			?>
			</ul>
		</div>
		<?php endif; ?>
		<form method="post" action="<?php echo WEBROOT ?>/register" id="registerform">
			<div>
				<input type="hidden" name="action" value="registerNewBlog" />
				<input type="hidden" name="user_login" id="user_login" value="<?php echo $user_login; ?>" />
				<input type="hidden" name="user_email" id="user_email" value="<?php echo $user_email; ?>" />
			</div>
			<div id="blog_name_span">
				<div id="blognameMsg" style="height:3em"></div>
				<label for="blog_name"><?php _e('New Blog Address:') ?></label><br />
				<input type="text" name="blog_name" id="blog_name" size="20" maxlength="100" value="<?php echo $blog_name; ?>" <?php echo SUBDOMAINS ? 'style="text-align: center;"' : ''?>/><br/>
				<label for="password"><?php _e('Verify your current password:') ?></label><br />
				<input type="password" name="user_password" id="user_password" size="20" maxlength="30" />
			</div><br/>
			<p class="submit"><input type="submit" value="<?php _e('Register') ?> &raquo;" id="submit" name="submit" /></p>
		</form>

	<script type="text/javascript">
	<!--
	function checkFeedback(msgName, elementID, check, timeout) {
		var msg = document.getElementById(msgName).innerHTML;
		var loginMsg = msg.substring(msg.indexOf('>')+1, msg.indexOf(':')-1);
		var inputValue = document.getElementById(elementID).value;
		if (loginMsg != inputValue)
		{
			new Ajax.Updater(msgName,'<?php echo WEBROOT ?>/ajax/availability.php', {
				asynchronous : true,
 				method : 'post',
					parameters : 'action=' + check + '&name=' + inputValue
				});
			setTimeout('checkFeedback("' + msgName + '","' + elementID + '","' + check + '",' + timeout*2 + ');',timeout);
		}
	};
	
	new Form.Element.Observer('blog_name', 0.5, 
		function(element, value) {
 			if(document.getElementById('blog_name').value.indexOf('&') > 0)
	 			document.getElementById('blognameMsg').innerHTML ='<span style=\"color: red;\">'+document.getElementById('blog_name').value+': Invalid blog address<\/span>';
 			else
				new Ajax.Updater('blognameMsg','<?php echo WEBROOT ?>/ajax/availability.php', {
					asynchronous : true,
		 			method : 'post',
		 			postBody : 'action=checkBlogname&name=' + value
	 			});
 			setTimeout('checkFeedback("blognameMsg","blog_name","checkBlogname",1000);',500);
		}
	);
	-->
	</script>
		<ul>
			<li><a href="<?php echo LURL; ?>/" title="<?php _e('Are you lost?') ?>">&laquo; <?php _e('Back to front page') ?></a></li>
			<li><a href="profile"><?php _e('View Profile'); ?></a></li>
		</ul>
	</div>

	</body>
	</html>
	<?php
	break;

case 'register':

	$user_login = sanitize_user( $_POST['user_login'] );
	$user_email = $_POST['user_email'];
	$blog_name = $_POST['blog_name'];
	$create_blog = $_POST['create_blog'];
	
	$errors = array();
		
	if ( $user_login == '' )
		$errors['user_login'] = __('<strong>ERROR</strong>: Please enter a username.');

	/* checking e-mail address */
	if ($user_email == '') {
		$errors['user_email'] = __('<strong>ERROR</strong>: Please type your e-mail address.<br/>');
	} else if (!is_email($user_email)) {
		$errors['user_email'] = __('<strong>ERROR</strong>: The email address isn&#8217;t correct.<br/>');
	} else if (1 != $user_admin && !preg_match(get_option('email_structure'), $user_email)){
		$errors['user_email'] = '<strong>ERROR</strong>: '.get_option('email_structure_error_message').'<br/>';
	} else if (get_option('registration_list') && !in_array($user_email, explode(',', get_option('registration_list')))) {
		$errors['user_email'] = '<strong>ERROR</strong>: That email address is not allowed to register on '.get_settings('site_title').'.<br/>';
		$user_email = '';
	}

	/* checking the email isn't already used by another user */
	$email_exists = $wpdb->get_row("SELECT user_email FROM $wpdb->users WHERE user_email = '$user_email'");
	if ( $email_exists)
		$errors['user_email'] = '<strong>ERROR</strong>: This email address is already registered, please supply another.';
		
	if ( ! validate_username($user_login) ) {
		$errors['user_login'] = __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.<br/>');
		$user_login = '';
	}

	if ( username_exists( $user_login ) )
		$errors['user_login'] = __('<strong>ERROR</strong>: This username is already registered, please choose another one.<br/>');

	//this could be a little cleaner. future wp versions clean up this file, so after merging with 2.1, a refactor is in order.
	if ('yes' == $create_blog){
		if ( !slugAvailable($blog_name) )
			$errors['blog_name'] = __('<strong>ERROR</strong>: This blog address is already taken, please choose another one.<br/>');
		if (!validateSlug($blog_name))
			$errors['blog_name'] = __('<strong>ERROR</strong>: This blog address is invalid, please choose another one.<br/>');
	}

	if ( 0 == count($errors) ) {
		$password = substr( md5( uniqid( microtime() ) ), 0, 7);

		$user_id = wp_create_user( $user_login, $password, $user_email );
		if ( ('yes' == $create_blog && get_settings('users_can_create_blogs') ) or $user_admin )//PEAK
			$blog_id = lyceum_create_blog( $user_id, $blog_name );
		if ( !$user_id || ('yes' == $create_blog && !$blog_id) )
			$errors['user_id'] = sprintf(__('<strong>ERROR</strong>: Couldn&#8217;t register you... please contact the <a href="mailto:%s">webmaster</a> !<br/>'), 'nobody@example.com');
		else
			wp_new_user_notification($user_id, $password);
	}
	
	if ( 0 == count($errors) ) {
			
	?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Lyceum &raquo; <?php _e('Registration Complete') ?></title>
	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?>" />	
	<link rel="stylesheet" href="<?php echo WEBROOT; ?>/wp-admin/wp-admin.css" type="text/css" />
	<style type="text/css">
	.submit {
		font-size: 1.7em;
	}
	</style>
</head>
<body>

<div id="login"> 
	<h2><?php _e('Registration Complete') ?></h2>
	<p><?php printf(__('Username: %s'), "<strong>" . wp_specialchars($user_login) . "</strong>") ?><br />
	<?php if ($user_admin): ?>
	<?php printf(__('Password: %s'), '<strong>' . __('emailed to new user') . '</strong>') ?> <br />
	<?php else: ?>
	<?php printf(__('Password: %s'), '<strong>' . __('emailed to you') . '</strong>') ?> <br />
	<?php endif; ?>
	<?php printf(__('E-mail: %s'), "<strong>" . wp_specialchars($user_email) . "</strong>") ?></p>
	<p class="submit"><a href="login"><?php _e('Login'); ?> &raquo;</a></p>
</div>
<?php 
if (LYCEUMDEBUG) echo $password; 
?>
</body>
</html>

		<?php
		break;
	}

default:

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Lyceum &raquo; <?php _e('Registration Form') ?></title>
	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?>" />
	<link rel="stylesheet" href="<?php echo WEBROOT; ?>/wp-admin/wp-admin.css" type="text/css" />
	<style type="text/css">
	#user_email, #user_login, #submit {
		font-size: 1.7em;
	}
	</style>
	<script src="<?php echo WEBROOT ?>/wp-includes/js/prototype.js" type="text/javascript"></script>
	<script type="text/javascript" language="javascript">
	function show_hide(idpass_id, basedon_id)
	{
		var idpass = null;
		if ((idpass = document.getElementById(idpass_id)) && (basedon = document.getElementById(basedon_id)))
			idpass.style.visibility = (basedon.checked ) ? 'visible' : 'hidden';
	}
	</script>

</head>

<body>
<div id="login">
<h1><a href="http://lyceum.ibiblio.org/">Lyceum</a></h1>
<?php if ($user_admin): ?>
<h2><?php _e('Create a new account') ?></h2>
<?php else: ?>
<h2><?php _e('Register for an account') ?></h2>
<?php endif; ?>

<?php if ( isset($errors) ) : ?>
<div class="error">
	<ul>
	<?php
	foreach($errors as $error) echo "<li>$error</li>";
	?>
	</ul>
</div>
<?php endif; ?>
<form method="post" action="<?php echo WEBROOT ?>/register" id="registerform">
	<div><input type="hidden" name="action" value="register" /></div>
	<div id="usernameMsg" style="float:right"></div>
	<p><label for="user_login"><?php _e('Username:') ?></label><br /> <input type="text" name="user_login" id="user_login" size="20" maxlength="20" value="<?php echo attribute_escape($user_login); ?>" /><br /></p>
	<p><label for="user_email"><?php _e('E-mail:') ?></label><br /> <input type="text" name="user_email" id="user_email" size="20" maxlength="100" value="<?php echo attribute_escape($user_email); ?>" /></p>
	<p><?php _e('A password will be emailed to you.') ?></p>
	
	<?php if(get_settings('users_can_create_blogs') or  $user_admin){ //PEAK
	?>
	
	<p>
		<label for="create_blog"><?php _e('Create Blog?') ?></label>
		<input type="checkbox" name="create_blog" id="create_blog" value="yes" <?php echo $create_blog ? 'checked="checked"' : '' ?> onclick="show_hide('blog_name_span','create_blog');show_hide('blog_name','create_blog');"/>
	</p>
	<div id="blog_name_span" style="visibility:<?php echo $create_blog ? 'visible' : 'hidden' ?>;">
		<div id="blognameMsg" style="height:3em"></div>
		<label for="blog_name"><?php _e('Blog Address:') ?></label><br />
		<input style="visibility:<?php echo $create_blog ? 'visible' : 'hidden' ?>;" type="text" name="blog_name" id="blog_name" size="20" maxlength="100" value="<?php echo $blog_name; ?>" />
	</div>

	<?php } ?>
	<p class="submit"><input type="submit" value="<?php _e('Register') ?> &raquo;" id="submit" name="submit" /></p>
</form>

<script type="text/javascript">
<!--

	function checkFeedback(msgName, elementID, params, timeout) {
		var msg = document.getElementById(msgName).innerHTML;
		var loginMsg = msg.substring(msg.indexOf('>')+1, msg.indexOf(':')-1);
		var inputValue = document.getElementById(elementID).value;
		if (loginMsg != inputValue)
		{
			new Ajax.Updater(msgName,'<?php echo WEBROOT ?>/ajax/availability.php', {
				asynchronous : true,
 				method : 'post',
					parameters : params + inputValue
				});
			setTimeout('checkFeedback("' + msgName + '","' + elementID + '","' + params + '",' + timeout*2 + ');',timeout);
		}
	};
	
	new Form.Element.Observer('user_login', 0.5, 
		function(element, value) {
 			if(document.getElementById('user_login').value.indexOf('&') > 0)
	 			document.getElementById('usernameMsg').innerHTML ='<span style=\"color: red;\">'+document.getElementById('user_login').value+': Invalid username<\/span>';
 			else
				new Ajax.Updater('usernameMsg','<?php echo WEBROOT ?>/ajax/availability.php', {
					asynchronous : true,
	 				method : 'post',
 					parameters : 'action=checkLogin&login='+ value
 				});
 			checkFeedback("usernameMsg","user_login","action=checkLogin&login=",1000);
		}
	);
	

	new Form.Element.Observer('blog_name', 0.5, 
		function(element, value) {
 			if(document.getElementById('blog_name').value.indexOf('&') > 0)
	 			document.getElementById('blognameMsg').innerHTML ='<span style=\"color: red;\">'+document.getElementById('blog_name').value+': Invalid blog address<\/span>';
 			else
				new Ajax.Updater('blognameMsg','<?php echo WEBROOT ?>/ajax/availability.php', {
					asynchronous : true,
		 			method : 'post',
		 			postBody : 'action=checkBlogname&name=' + value
	 			});
 			setTimeout('checkFeedback("blognameMsg","blog_name","action=checkBlogname&name=",1000);',500);
		}
	);
-->
</script>
<ul>
	<li><a href="<?php echo LURL; ?>/" title="<?php _e('Are you lost?') ?>">&laquo; <?php _e('Back to front page') ?></a></li>
	<li><a href="<?php echo LURL; ?>/login"><?php _e('Login') ?></a></li>
	<li><a href="<?php echo LURL; ?>/lost-pass.php?action=lostpassword" title="<?php _e('Password Lost and Found') ?>"><?php _e('Lost your password?') ?></a></li>
</ul>
</div>

</body>
</html>
<?php

break;

case 'disabled':

	?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Lyceum &raquo; <?php _e('Registration Currently Disabled') ?></title>
	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?>" />
	<link rel="stylesheet" href="<?php echo WEBROOT; ?>/wp-admin/wp-admin.css" type="text/css">
</head>

<body>

<div id="login">
	<h2><?php _e('Registration Disabled') ?></h2>
	<p><?php _e('User registration is currently not allowed.') ?><br />
	<a href="<?php echo get_settings('home'); ?>/" title="<?php _e('Go back to the blog') ?>"><?php _e('Home') ?></a>
	</p>
</div>

</body>
</html>

	<?php
break;

case 'blogdisabled':

	?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Lyceum &raquo; <?php _e('Blog Creation is Disabled') ?></title>
	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?>" />
	<link rel="stylesheet" href="<?php echo WEBROOT; ?>/wp-admin/wp-admin.css" type="text/css">
</head>

<body>

<div id="login">
	<h2><?php _e('Blog Creation Disabled') ?></h2>
	<p><?php _e('Only Site Admins can create Blogs for you.') ?><br />
	<a href="<?php echo get_settings('home'); ?>/" title="<?php _e('Go back to the blog') ?>"><?php _e('Home') ?></a>
	</p>
</div>

</body>
</html>

	<?php
break;

case 'toomanyblogs':

	?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Lyceum &raquo; <?php _e('Blogs limit reached') ?></title>
	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?>" />
	<link rel="stylesheet" href="<?php echo WEBROOT; ?>/wp-admin/wp-admin.css" type="text/css">
</head>

<body>

<div id="login">
	<h2><?php _e('Blog limit reached.') ?></h2>
	<p><?php _e('Sorry, you can only create '.get_settings('users_can_create_num_blogs').' blogs.') ?><br />
	<a href="<?php echo get_settings('home'); ?>/" title="<?php _e('Go back to the blog') ?>"><?php _e('Home') ?></a>
	</p>
</div>

</body>
</html>

	<?php
break;

}
?>
